import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:delivery_application/common_widget/round_button.dart';
import 'package:delivery_application/common_widget/round_textfield.dart';
import 'package:delivery_application/view/home/delivery_map_view.dart';
import 'package:delivery_application/view/home/success_deliver.dart';
import 'package:delivery_application/view/login/rest_password_view.dart';
import 'package:delivery_application/view/login/sign_up_view.dart';

class PayView extends StatefulWidget {
  final double? latitude;
  final double? longitude;
  
  const PayView({
    super.key, 
    this.latitude, 
    this.longitude
  });

  @override
  State<PayView> createState() => _PayViewState();
}

class _PayViewState extends State<PayView> {
  TextEditingController txtordernumber = TextEditingController();
  TextEditingController txtaddress = TextEditingController();
  bool _hasCoordinates = false;
  
  @override
  void initState() {
    super.initState();
    // 验证坐标是否存在
    _hasCoordinates = widget.latitude != null && widget.longitude != null;
    print("PayView初始化: 坐标信息存在=$_hasCoordinates, 坐标=(${widget.latitude}, ${widget.longitude})");
  }
  
  void _navigateToDeliveryMap() {
    if (_hasCoordinates) {
      print("准备跳转到配送地图页面，坐标: ${widget.latitude}, ${widget.longitude}");
      
      // 如果有坐标信息，跳转到配送地图页面
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => DeliveryMapView(
            destinationLat: widget.latitude!,
            destinationLng: widget.longitude!,
          ),
        ),
      );
    } else {
      print("没有坐标信息，显示提示");
      
      // 显示提示对话框
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: Text("无法显示配送地图"),
          content: Text("您的订单缺少位置坐标信息，无法在地图上显示配送进度。请返回并通过地图选择按钮来指定位置。"),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text("返回"),
            ),
            TextButton(
              onPressed: () {
                Navigator.pop(context);
                // 直接跳转到成功页面
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const SuccessDiliver(),
                  ),
                );
              },
              child: Text("跳过地图动画"),
            ),
          ],
        ),
      );
    }
  }
  
  @override
  Widget build(BuildContext context) {
    var media = MediaQuery.of(context).size; // 获取当前设备的屏幕尺寸信息
    return Scaffold(
      backgroundColor: Color(0xff00AEEE),
      appBar: AppBar(
        title: Text("订单支付", style: TextStyle(color: Colors.white)),
        backgroundColor: Color(0xff00AEEE),
        elevation: 0,
        iconTheme: IconThemeData(color: Colors.white),
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.symmetric(
              vertical: 20, horizontal: 25),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              // 第一个图片居中
              Center(
                child: Image.asset(
                  "assets/img/2.png",
                  width: media.width * 0.5,
                  height: media.height * 0.2,
                  fit: BoxFit.contain,
                ),
              ),

              SizedBox(height: 20),

              // 订单状态信息卡片
              Container(
                padding: EdgeInsets.all(15),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(15),
                ),
                child: Column(
                  children: [
                    Row(
                      children: [
                        Icon(Icons.check_circle, color: Colors.green),
                        SizedBox(width: 10),
                        Text(
                          "订单已创建",
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ],
                    ),
                    Divider(height: 30),
                    Row(
                      children: [
                        Icon(
                          _hasCoordinates ? Icons.check_circle : Icons.info,
                          color: _hasCoordinates ? Colors.green : Colors.orange,
                        ),
                        SizedBox(width: 10),
                        Text(
                          _hasCoordinates ? "位置信息已确认" : "缺少位置信息",
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.bold,
                            color: _hasCoordinates ? Colors.black : Colors.orange,
                          ),
                        ),
                      ],
                    ),
                    if (!_hasCoordinates)
                      Padding(
                        padding: const EdgeInsets.only(top: 8, left: 34),
                        child: Text(
                          "无法在地图上显示配送路线，可以点击下方按钮直接查看订单进度",
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.orange,
                          ),
                        ),
                      ),
                  ],
                ),
              ),

              SizedBox(height: 40),

              // 第二个图片居中
              Center(
                child: Image.asset(
                  "assets/img/1.png",
                  width: media.width * 0.5,
                  height: media.height * 0.15,
                  fit: BoxFit.contain,
                ),
              ),

              SizedBox(height: 40),

              // 第三个图片居中
              Center(
                child: Image.asset(
                  "assets/img/3.png",
                  width: media.width * 0.5,
                  height: media.height * 0.15,
                  fit: BoxFit.contain,
                ),
              ),

              SizedBox(height: 20),

              // 坐标信息显示
              if (_hasCoordinates)
                Container(
                  padding: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    color: Colors.white.withOpacity(0.2),
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(Icons.location_on, color: Colors.white),
                      SizedBox(width: 10),
                      Text(
                        "配送地址坐标: (${widget.latitude!.toStringAsFixed(4)}, ${widget.longitude!.toStringAsFixed(4)})",
                        style: TextStyle(color: Colors.white),
                      ),
                    ],
                  ),
                ),

              SizedBox(height: 30),

              RoundButton(
                title: _hasCoordinates ? "查看配送地图" : "查看订单进度",
                onPressed: _navigateToDeliveryMap,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
